package com.junyu.exception;

import com.junyu.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * 全局异常处理器类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     *捕获Exception类型的异常
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        if(e != null){
            log.error("出现异常：{}", ExceptionUtils.getStackTrace(e));
        }
        return Result.fail();
    }

    /**
     *捕获LoginException类型的异常
     */
    @ExceptionHandler(LoginException.class)
    public Result exception(LoginException e){
        if(e.getE() != null){
            log.error("出现异常：{}", ExceptionUtils.getStackTrace(e.getE()));
        }
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }
}